package entity;



/**
 * The Class Time.
 */
public class TimeSlot {

	/** The _start time. */
	private int _startTime;
	
	/** The _duration minute. */
	private int _durationMinute;
	
	/**
	 * Instantiates a new time.
	 *
	 * @param time (e.g. 9:00)
	 * @param duration (e.g. 30)
	 */
	public TimeSlot (String time, int duration) {
		this._startTime = stringToMinute(time);
		
		//this._startTime = Integer.parseInt(time);
		this._durationMinute = duration;
	}
	
	// this._startTime		+ _durationMinute		1000+800 v 1000+900
	// that_time _startTime	+ _durationMinute		1800+100 v 1800+100
	/**
	 * Checks if it is collided with another time.
	 *
	 * @param that_time another time
	 * @return true, if it is collided with another time.
	 */
	public boolean isCollideWith(TimeSlot that_time) {
		if((this._startTime+this._durationMinute)==(that_time._startTime+that_time._durationMinute))
            return true;
		else if(this._startTime==that_time._startTime)
            return true;
		else if((this._startTime<that_time._startTime) && ((this._startTime+this._durationMinute)>= that_time._startTime))
            return true;
		else if ((this._startTime>that_time._startTime) && ((that_time._startTime+that_time._durationMinute)>= this._startTime))
			return true;
		else 
			return false;
	}
	
	/**
	 * Gets the duration in minute.
	 *
	 * @return the duration in minute
	 */
	public int getDurationInMinute(){
		return _durationMinute;
	}
	
	
	/**
	 * Gets the end time. (Caveat: Cannot handle midnight time addition)
	 *
	 * @return the end time
	 */
	public String getEndTime() {
		String endTime = (this._startTime+this._durationMinute)/60 + ":";
		
		if(((this._startTime+this._durationMinute)%60)<10)
			endTime += "0";
		
		endTime += (this._startTime+this._durationMinute)%60;
		
		return endTime;
	}
	
	/**
	 * String to minute.
	 *
	 * @param timeString (e.g. 9:00)
	 * @return the int	 (e.g. 540)
	 */
	public int stringToMinute(String timeString) {
		String[] timeStringArray = timeString.split(":");
		return Integer.parseInt(timeStringArray[0]) * 60 + Integer.parseInt(timeStringArray[1]);
		
	}
	
	/**
	 * Minute to string.
	 *
	 * @param int			(e.g. 540)
	 * @return timeString	(e.g. 9:00)
	 */
	public String minuteToString(int time) {
		String timeString = time/60 + ":";
		if(time%60<10)
			timeString += "0";
		
		timeString += time%60;
		return timeString;
		
	}
	
}